package io.renren.generator;

import java.sql.SQLException;

import org.junit.Test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


public class EntityServiceGenerator {

    @Test
    public void generateCode(){
//        doGenerateCode("tbl_order_items","tbl_prd","tbl_user_addr");
        doGenerateCode("tbl_shop_cart");
    }

    /**
     * 生成实体和Service代码。
     * 生成页面需要另外一个项目renre-generate
     * @throws SQLException
     */
    private void doGenerateCode(String... tableNames) {
        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://localhost:3306/renren_fast?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8";
//        DataSource dataSource = new SimpleDriverDataSource(new Driver(),
//                dbUrl, "root","perlight!@#321"
//                );
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("123456")
                .setDriverName("com.mysql.jdbc.Driver");
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setSuperEntityClass("io.renren.modules.front.base.BaseTO")
                .setCapitalMode(true)
                .setEntityLombokModel(true)
                .setDbColumnUnderline(true)
                .setNaming(NamingStrategy.underline_to_camel)
                .setSuperEntityColumns(new String[]{"createBy","createTime","updateBy","updateTime","delFlag"})
                .setInclude(tableNames);//修改替换成你需要的表名，多个表名传数组
        config.setActiveRecord(false)
                .setAuthor("yuxiaobin")
                .setOutputDir("d:/test")
                .setFileOverride(true);
//                .setControllerName(null)

//                .setServiceName("OrderService");
        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                                .setParent("io.renren.modules")
                                .setModuleName("front")
                                .setController("controller")
                                .setEntity("entity")
        ).execute();
    }
}
